package com.lib.service;

import cn.hutool.core.util.StrUtil;
import com.lib.dao.AdminDao;
import com.lib.dao.ex.DaoException;
import com.lib.dao.ex.UpdateDaoException;
import com.lib.entity.Admin;
import com.lib.service.ex.*;

public class AdminService {
    private AdminDao adminDao = new AdminDao();

    //管理员登录
    public Admin login(String name, String pwd) throws EmptyArgumentException, LoginException, ServiceException {
        if (StrUtil.isEmpty(name)) {
            throw new EmptyArgumentException("登录异常,用户名不能为空");
        }
        if (StrUtil.isEmpty(pwd)) {
            throw new EmptyArgumentException("登录异常,密码不能为空");
        }
        Admin admin = null;
        try {
            admin = adminDao.selectAdminByName(name);
        } catch (DaoException e) {
            throw new ServiceException("数据库异常" + e.getMessage(), e);
        }
        if (admin == null) {
            throw new LoginException("登录异常,用户不存在");
        }
        if (!admin.getPwd().equals(pwd)) {
            throw new LoginException("登录异常,密码错误");
        }
        admin.setPwd("密码密码");
        return admin;
    }

    //检验是否为旧密码
    public Admin isOldPwd(String name, String opwd) throws EmptyArgumentException, NotOldPwdException, RecordNotFoundException, ServiceException {
        if (StrUtil.isEmpty(name)) {
            throw new EmptyArgumentException("用户名不能为空");
        }
        if (StrUtil.isEmpty(opwd)) {
            throw new EmptyArgumentException("密码不能为空");
        }
        Admin admin = null;
        try {
            admin = adminDao.selectAdminByName(name);
        } catch (DaoException e) {
            throw new ServiceException("验证旧密码:数据库异常" + e.getMessage(), e);
        }
        if (admin == null) {
            throw new RecordNotFoundException("验证旧密码:用户记录不存在");
        }
        if (!opwd.equals(admin.getPwd())) {
            throw new NotOldPwdException("验证旧密码:密码不匹配");
        }
        return admin;
    }
    //修改管理员密码
    public void changePwd(String name, String opwd, String npwd, String cpwd) throws  RecordNotFoundException, InValidArgumentException, EmptyArgumentException, UpdateServiceException, NotOldPwdException,ServiceException {
        if (StrUtil.isEmpty(name)) {
            throw new EmptyArgumentException("修改密码:用户名不能为空");
        }
        if (StrUtil.isEmpty(opwd)) {
            throw new EmptyArgumentException("修改密码:旧密码不能为空");
        }
        if (StrUtil.isEmpty(npwd)) {
            throw new EmptyArgumentException("修改密码:新密码不能为空");
        }
        if(!npwd.equals(cpwd)){
            throw new InValidArgumentException("修改密码:确认密码与新密码不同");
        }
        Admin admin = null;
        try {
            admin = adminDao.selectAdminByName(name);
        } catch (DaoException e) {
            throw new ServiceException("修改密码：数据库错误"+e.getMessage(),e);
        }
        if(admin==null){
            throw new RecordNotFoundException("修改密码:用户记录不存在");
        }
        if( !opwd.equals(admin.getPwd())){
            throw new NotOldPwdException("修改密码:旧密码不正确");
        }

        try {
            adminDao.updatePwd(name,npwd);
        } catch (DaoException e) {
            throw new ServiceException("修改密码:修改失败:"+e.getMessage(),e);
        } catch (UpdateDaoException e) {
            throw new UpdateServiceException("修改密码:数据库异常:"+e.getMessage(),e);
        }
    }
}
